package org.example.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.enums.BizCodeEnum;
import org.example.model.PageResult;
import org.example.request.LockCouponRecordRequest;
import org.example.service.CouponRecordService;
import org.example.util.JsonData;
import org.example.vo.CouponRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LinN
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/api/coupon_record/v1")
@Api(tags = "优惠券记录模块")
public class CouponRecordController {

    @Autowired
    private CouponRecordService couponRecordService;

    @ApiOperation("分页查询用户优惠券记录")
    @GetMapping("page")
    public JsonData page(@ApiParam(value = "当前页码", required = true) @RequestParam(defaultValue = "1") int page,
                         @ApiParam(value = "每页显示多少条", required = true) @RequestParam(defaultValue = "10") int size) {
        PageResult pageResult = couponRecordService.page(page, size);
        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("查询优惠券详情")
    @GetMapping("detail/{record_id}")
    public JsonData detail(@ApiParam(value = "记录id", readOnly = true)
                           @PathVariable(value = "record_id", required = true) Long recordId) {
        CouponRecordVO couponRecordVO = couponRecordService.findById(recordId);
        return couponRecordVO == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS) : JsonData.buildSuccess(couponRecordVO);
    }

    @ApiOperation("rpc-锁定优惠券记录")
    @PostMapping("lock_records")
    public JsonData lockCouponRecords(@ApiParam("锁定优惠券记录请求对象")
                                      @RequestBody LockCouponRecordRequest recordRequest) {
        JsonData jsonData = couponRecordService.lockCouponRecords(recordRequest);
        return jsonData;
    }

}

